﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;
using Resources;
using Hutech.Authentication.Library.Business.WebHelpers;
using log4net;
using Hutech.Authentication.Library.Business;
using System.Globalization;

namespace Hutech.CRM.Web.Admin
{
    public partial class MemberList : hutechBasePage
    {
        #region Properties
        protected int pageNumber
        {
            get
            {
                if (ViewState["pageNumber"] != null)
                {
                    try { return Convert.ToInt32(ViewState["pageNumber"].ToString()); }
                    catch { return 1; } // add
                }
                return 1;
            }
            set
            {
                ViewState["pageNumber"] = value;
            }
        }
        public int pageSize
        {
            get
            {
                if (ViewState["PageSize"] != null)
                {
                    try { return Convert.ToInt32(ViewState["PageSize"].ToString()); }
                    catch { return 25; } // add
                }
                return 25;
            }
            set
            {
                ViewState["PageSize"] = value;
            }
        }
        private int totalPages = 1;
        #endregion

        #region Variable
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            pgrItems.Command += new System.Web.UI.WebControls.CommandEventHandler(pgrItems_Command);

            rptUsers.ItemCommand += new RepeaterCommandEventHandler(rptUsers_ItemCommand);
            rptUsers.ItemDataBound += new RepeaterItemEventHandler(rptUsers_ItemDataBound);

            btnSearchUser.Click += new EventHandler(btnSearchUser_Click);
        }

               
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebUser.IsAdminOrRoleAdmin)
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }
            PopulateLabels();
            PopulateControls();
        }

        #region Load All data
        private void PopulateLabels()
        {
            Title = Resource.AdminMenuRoleAdminLink;
            spnTitle.InnerText = Resource.MemberListLink;

            lnkAdminMenu.Text = Resource.AdminMenuLink;
            lnkAdminMenu.ToolTip = Resource.AdminMenuLink;
            lnkAdminMenu.NavigateUrl = SiteRoot + "/Admin/AdminMenu.aspx";

            btnSearchUser.Text = Resource.SearchButtonText;
            btnSearchUser.ToolTip = Resource.SearchButtonText;

            LableName.Text = Resource.MemberListUserNameLabel;
            LableLoginName.Text = Resource.MemberListLoginNameLabel;
            LableEmail.Text = Resource.MemberListEmailLabel;

            lnkNewUser.Visible = true;
            lnkNewUser.Text = Resource.MemberListAddUserLabel;
            lnkNewUser.NavigateUrl = SiteRoot + "/Secure/Register.aspx";
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack)
                return;
            BindRepeater();
        }
        private void BindRepeater()
        {
            this.rptUsers.DataSource = SiteUser.GetAllUser(pageNumber, pageSize, out totalPages);
            this.rptUsers.DataBind();

            if (this.totalPages > 1)
            {
                pgrItems.ShowFirstLast = true;
                pgrItems.PageSize = this.pageSize;
                pgrItems.PageCount = this.totalPages;
            }
            else
            {
                pgrItems.Visible = false;
            }
        }
        //private void LoadParams()
        //{
        //    roleID = WebUtils.ParseGuidFromQueryString("roleid", Guid.Empty);
        //}
        #endregion

        #region All event
        void rptUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //if (e.CommandName == "delete")
            //{
            //    Guid userId = Guid.Parse(e.CommandArgument.ToString());
            //    SiteUser user = new SiteUser(userId);
            //    // Neu so luong role cua user lon hon 1 thi them moi.
            //    //Neu bang 1 ta thay doi quyen hien tai thanh quyen moi. Vi khi = 1, user co quyen UnAuthenticated
            //    if (Role.CountRoleByUser(user.UserId) > 1)
            //        Role.RemoveUser(roleID, user.UserId);
            //    else
            //        Role.ChangeUserToNotAuthenticated(user.UserId);
            //}
            //BindRepeater();
            //this.pnlMain.Update();
            ////WebUtils.SetupRedirect(this, Request.RawUrl);
        }
        void rptUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
            //if (btnDelete != null)
            //{
            //    btnDelete.AlternateText = Resource.ManageUsersRemoveFromRoleButton;
            //    btnDelete.ToolTip = Resource.ManageUsersRemoveFromRoleButton;
            //    UIHelper.AddConfirmationDialog(btnDelete, Resource.RolesRemoveUserWarning);
            //}
        }       
        void pgrItems_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            pageNumber = Convert.ToInt32(e.CommandArgument);
            pgrItems.CurrentIndex = pageNumber;
            BindRepeater();
        }
        void btnSearchUser_Click(object sender, EventArgs e)
        {
            
        }
        #endregion
    }
}
